import axios from 'axios';


// 通用公用方法
const req = (method, url, params) => {
  if (window.localStorage.getItem('token') == null) {
    return axios({
      method: method,
      url: url,
      headers: {
        'Content-Type': 'application/json'
      },
      data: params

    }).then(res => res.data);
  } else {
    return axios({
      method: method,
      url: url,
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer' + window.localStorage.getItem('token')
      },
      data: params
      // traditional: true,
      // transformRequest: [
      //     function(data) {
      //         let ret = ''
      //         for (let it in data) {
      //             ret +=
      //                 encodeURIComponent(it) +
      //                 '=' +
      //                 encodeURIComponent(data[it]) +
      //                 '&'
      //         }
      //         return ret
      //     }
      // ]
    }).then(res => {
      if(res.data.message == 'token is blacklist') {
        localStorage.removeItem('userInfo');
      } else {
        return res.data
      }
    });
  }
};

export {
  req
}
